﻿using Ec.NETCoreFramework.Common.Core;
using Ec.NETCoreFramework.Common.Core.Helpers;
using Ec.NETCoreFramework.Common.MQ;
using Ec.NETCoreFramework.Common.Core.Utilities;
using Ec.NETCoreFramework.Common.Core.Serializer;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Threading;
using System.Diagnostics;
using Ec.NETCoreFramework.Common;
using Ec.NETCoreFramework.Common.Plug.Core;
using Ec.NETCoreFramework.Common.Clients;

namespace MQ_WechatMedia
{
    /// <summary>
    /// media 素材
    /// </summary>
    public class Media : TaskInterface
    {
        private ConsumeConfig _consumeConfig;
        public override async Task ConsumeEvent(byte[] bytes)
        {
            if (_consumeConfig == null)
            {
                Console.WriteLine("对象为空");
                return;
            }

            var dic = ByteSerializer.Deserialize<Dictionary<string, object>>(bytes);

            var appSecret = new RequestAppSecret()
            {
                AppId = _consumeConfig.AppId,
                Secret = _consumeConfig.Secret
            };
            var actoin = Utils.ToString(dic["action"]);
            IMedia bll = null;
            if (actoin == MediaEnum.Journal.ToString())
            {
                bll = new JournalMedia(appSecret, _consumeConfig);
            }
            else if (actoin == MediaEnum.MaterialMedia.ToString())
            {
                bll = new MaterialMedia(appSecret, _consumeConfig);
            }
            else if (actoin == MediaEnum.Approval.ToString())
            {
                bll = new ApprovalMedia(appSecret, _consumeConfig);
            }
            if (bll != null)
            {
                await bll.Excute(dic);
            }
        }
 
      
        private string _QueueName = string.Empty;
        public override string QueueName()
        {
            return "wechat-" + _consumeConfig.MQName;
        }
        public override void SetConsumeConfig(byte[] consumeConfig)
        {
            _consumeConfig = ProtoBufSerializer.DeSerialize<ConsumeConfig>(consumeConfig);
        }
    }

}
